package com.frontop.system.modules.sys.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frontop.common.util.ResultJsonUtil;
import com.frontop.system.modules.sys.model.SysMenu;
import com.frontop.system.modules.sys.model.SysOrganizationMenu;
import com.frontop.system.modules.sys.service.SysMenuService;
import com.frontop.system.modules.sys.service.SysOrganizationMenuService;
import com.frontop.system.modules.sys.vo.MenuManageListVO;
import com.frontop.system.modules.sys.vo.MenuVO;
import com.frontop.system.modules.user.service.TUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  菜单
 * </p>
 *
 * @author YangJia
 * @since 2021-09-15
 */
@RestController
@RequestMapping("/sys-menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private TUserService tUserService;
    @Autowired
    private SysOrganizationMenuService sysOrganizationMenuService;
    @ApiOperation(value = "获取菜单列表")
    @GetMapping("/getMenuList")
    public ResultJsonUtil<Object> getMenuList(Authentication authentication,HttpServletRequest request) {
        String userName  = tUserService.getThisLoginUserName(authentication);
        List<MenuVO> menuVOS = sysMenuService.getListMenuVO(userName,1);
        return new ResultJsonUtil().success(menuVOS);
    }

    @ApiOperation(value = "获取可选菜单列表（包括按钮等）")
    @GetMapping("/getAllMenuLis")
    public ResultJsonUtil<Object> getAllMenuLis(Authentication authentication,HttpServletRequest request) {
        String userName  = tUserService.getThisLoginUserName(authentication);
        List<MenuVO> menuVOS = sysMenuService.getListMenuVO(userName,0);
        return new ResultJsonUtil().success(menuVOS);
    }

    @ApiOperation(value = "获取菜单树管理列表")
    @GetMapping("/getMenuManageList")
    public ResultJsonUtil<Object> getMenuManageList(HttpServletRequest request) {
        List<MenuManageListVO> menuManageList = sysMenuService.getMenuManageList(null);
        return new ResultJsonUtil().success(menuManageList);
    }

    @ApiOperation(value = "根据组织ID获取当前组织下所有菜单")
    @GetMapping("/selectMenuByOrg/{orgId}")
    public ResultJsonUtil<Object> selectMenuByOrg(@PathVariable("orgId") Integer orgId,HttpServletRequest request) {
        List<MenuManageListVO> menuManageList = sysMenuService.selectMenuByOrg(orgId);
        return new ResultJsonUtil().success(menuManageList);
    }



    @ApiOperation(value = "获取菜单信息")
    @GetMapping("/getMenuInfoById/{id}")
    public ResultJsonUtil<Object> getMenuInfoById(@PathVariable("id") Integer id, HttpServletRequest request) {

        SysMenu sysMenu = sysMenuService.getMenuInfoById(id);
        return new ResultJsonUtil().success(sysMenu);
    }

    @ApiOperation(value = "添加或修改菜单")
    @PostMapping("/saveOrUpdateMenu")
    public ResultJsonUtil<Object> saveOrUpdateMenu(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
        if(sysMenu.getMenuId() != null && sysMenu.getParentId().equals(sysMenu.getMenuId())){
            return new ResultJsonUtil().fail("上级菜单不能选择当前菜单!");
        }
        boolean isAdd = false;
        if(sysMenu.getMenuId() != null){
            Map<String, Integer> map = new HashMap<>();
            map.put("menuId",sysMenu.getMenuId());
            map.put("parentId",sysMenu.getParentId());
            Integer count = sysMenuService.isParentInChild(map);
            if(count != null && count > 0){
                return new ResultJsonUtil().fail("上级菜单不能选择当前菜单的子节点!");
            }
        }else {
            if(sysMenu.getParentId() == null){
                sysMenu.setParentId(0);
            }
            isAdd = true;
        }
        sysMenuService.saveOrUpdate(sysMenu);

        if(isAdd){
            //默认菜单赋予总部企业权限
            SysOrganizationMenu sysOrganizationMenu = new SysOrganizationMenu();
            sysOrganizationMenu.setOrgId(1);
            sysOrganizationMenu.setMenuId(sysMenu.getMenuId());
            sysOrganizationMenuService.save(sysOrganizationMenu);
        }

        return new ResultJsonUtil().success("保存成功");
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/deleteMenuById/{id}")
    public ResultJsonUtil<Object> deleteMenuById(@PathVariable("id") Integer id, HttpServletRequest request) {
        List<Integer> list = sysMenuService.getAllChildById(id);
        list.add(id);
        sysMenuService.removeByIds(list);
        return new ResultJsonUtil().success("删除成功");
    }



}